const {
  getMyInfo,
  updataNickname,
  upSex,
  upavatar,
} = require("../../api/myApi");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    nicknamePopover: false, // 修改昵称弹窗
    sexPopover: false, // 修改性别弹窗
    newNickname: "", // 用户新昵称
    token: "",
    avatarUrl: "", // 头像地址
  },

  infoJt() {
    wx.switchTab({
      url: "../my/my",
    });
  },

  async upSexHandle(data) {
    const res = await upSex(data);
  },

  // 获取用户信息
  async getMyInfoHandle(data) {
    const res = await getMyInfo(data);
    this.setData({
      userInfo: res.data.data.user,
    });
  },

  // 点击修改昵称
  updataNickname() {
    this.setData({
      nicknamePopover: true,
    });
  },

  // 输入框
  nicknameInput(e) {
    this.setData({
      newNickname: e.detail.value,
    });
  },

  // 弹窗确定按钮
  async nicknameBtn() {
    const data = {
      token: this.data.token,
      nickname: this.data.newNickname,
    };
    const res = await updataNickname(data);
    this.setData({
      nicknamePopover: false,
    });

    this.getMyInfoHandle({ token: this.data.token });
  },

  // 取消按钮
  nicknameQuxiao() {
    this.setData({
      nicknamePopover: false,
    });
  },

  // 修改性别按钮
  upSex() {
    this.setData({
      sexPopover: true,
    });
  },

  // 男
  upNan() {
    this.setData({
      sexPopover: false,
    });
    this.upSexHandle({ token: this.data.token, gender: "male" });
    this.getMyInfoHandle({ token: this.data.token });
  },

  // 女
  upNv() {
    this.setData({
      sexPopover: false,
      newSex: "female",
    });
    this.upSexHandle({ token: this.data.token, gender: "female" });
    this.getMyInfoHandle({ token: this.data.token });
  },

  quxiao() {
    this.setData({
      sexPopover: false,
    });
  },

  // 修改头象
  async upavatarImg(data) {
    const res = await upavatar(data);
    this.getMyInfoHandle({ token: this.data.token });
  },

  // 上传头像
  upavatarHandle() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: "https://homekeep.sdzhuanbang.com/api/wxuser/upload_img",
          filePath: tempFilePaths[0],
          name: "file",
          formData: {
            token: that.data.token,
          },
          success(res) {
            const data = JSON.parse(res.data);
            that.upavatarImg({ token: that.data.token, avatar: data.data.url });
          },
        });
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const token = wx.getStorageSync("token");
    this.setData({
      token,
    });
    this.getMyInfoHandle({ token });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
